package com.exercise.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.exercise.dao.UserDao;
import com.exercise.model.InterimUser;
import com.exercise.model.User;


@Repository("userDao")
public class UserDaoImpl implements UserDao{

	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public int saveUser(User user) {
		
		Session session=sessionFactory.getCurrentSession();
		
		Serializable result=session.save(user);
		
		if ((Integer)result > 0) {
			return 1;
		}else {
			return 0;
		}
	}
	
	@Override
	public User selectchecknum(String unum) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where unum = ?");
		query.setParameter(0, unum);
		List<User> list = query.list();
		if (list != null && list.size() != 0) {
			User user = list.get(0);
			return user;
		}
		return null;
	}

	@Override
	public User selectlogin(User user) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where unum = ? and upwd = ?");
		query.setParameter(0, user.getUnum());
		query.setParameter(1, user.getUpwd());
		List<User> list = query.list();
		if (list != null && list.size() != 0) {
			User exuser = list.get(0);
			return exuser;
		}
		return null;
	}

	@Override
	public List<User> saveuseExam(InterimUser interimuser) {
		Session session = sessionFactory.getCurrentSession();
		
		String base = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
		Random random = new Random();
		List<User> useexam = new ArrayList<User>();
		int j,i;
		for ( j=interimuser.getMin();j < interimuser.getMax()+1; j++) {
			User user = new User();
			String addunum = "";
			for (i = String.valueOf(j).length(); i < String.valueOf(interimuser.getMax()).length(); i++) {
				addunum += "0";
			}
			addunum += String.valueOf(j);
			StringBuffer sb = new StringBuffer();
			for (i = 0; i < 8; i++) {
				int number = random.nextInt(base.length());
				sb.append(base.charAt(number));
			}

			user.setUnum(interimuser.getPrefix().concat(addunum));
			user.setUpwd(sb.toString());
			Serializable result = session.save(user);
			if ((Integer) result < 0) {
				return null;
			}
			useexam.add(user);
		}
		return useexam;
		/*RandomUser randomUser = new RandomUser();
		
		List<User> users = randomUser.CreateRandomUser(interimuser);
		for(User user:users) {
			Serializable result = session.save(user);
			if ((Integer) result < 0) {
				return null;
			}
		}
		return users;*/
	}

	@Override
	public int deleteuseExam() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete User where uname is null");
		int ifdelete = query.executeUpdate();
		return ifdelete;
	}

}
